package com.xzj.admin.analysis.base.custom.ty;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.ThemeSpecialAnalysisHandle;
import com.xzj.admin.analysis.base.custom.common.PlatformSpecialHandle;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import com.xzj.admin.analysis.base.domain.ResMessageManage;

/**
 * @program: xzj-admin
 * @ClassName: VcThemeSpecialAnalysisHandle
 * @description: IT-VC特殊主题处理器
 * @author: XiaZunJun
 * @create: 2024-11-20 14:23
 */
public class TyyThemeSpecialAnalysisHandle extends ThemeSpecialAnalysisHandle {

    static {
        putThemeSpecialHandle(buildKey("physicalHost", MESSAGE_TAG.add.name()), TyyThemeSpecialAnalysisHandle::physicalHostAdd);
        putThemeSpecialHandle(buildKey("physicalHost", MESSAGE_TAG.edit.name()), TyyThemeSpecialAnalysisHandle::physicalHostEdit);
        putThemeSpecialHandle(buildKey("physicalHost", MESSAGE_TAG.del.name()), TyyThemeSpecialAnalysisHandle::physicalHostDel);
        putThemeSpecialHandle(buildKey("virtualHost", MESSAGE_TAG.add.name()), TyyThemeSpecialAnalysisHandle::virtualHostAdd);
        putThemeSpecialHandle(buildKey("virtualHost", MESSAGE_TAG.edit.name()), TyyThemeSpecialAnalysisHandle::virtualHostEdit);
        putThemeSpecialHandle(buildKey("virtualHost", MESSAGE_TAG.del.name()), TyyThemeSpecialAnalysisHandle::virtualHostDel);
    }

    public TyyThemeSpecialAnalysisHandle(String cloudId, String cloudType) {
        super(cloudId, cloudType, message ->
                "physicalHost".equals(message.getMessageSource())
                        || "virtualHost".equals(message.getMessageSource())
        );
    }

    private static void physicalHostAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject jsonObject = message.getRowData();
        String key = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(),"serialNo");
        String serialNo = jsonObject.getString("serialNo");
        if (StrUtil.isNotEmpty(serialNo)) {
            redisHandle.setCacheMapValue(key, serialNo, message.getMessageId());
            redisHandle.setCacheMapValue(key, message.getMessageId(), serialNo);
        }
    }

    private static void physicalHostEdit(ResMessageManage message, CommonFlowInfo info){
        JSONObject jsonObject = message.getRowData();
        String key = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(),"serialNo");
        String serialNo = jsonObject.getString("serialNo");
        judgeUpdate(message, key, serialNo);
    }

    private static void physicalHostDel(ResMessageManage message, CommonFlowInfo info){
        String key = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(),"serialNo");
        judgeDel(message, key);
    }

    private static void virtualHostAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject jsonObject = message.getRowData();
        String key = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(), "outbindIp");
        String outBindIp = jsonObject.getString("outbindIp");
        if (StrUtil.isNotEmpty(outBindIp)) {
            redisHandle.setCacheMapValue(key, outBindIp, message.getMessageId());
            redisHandle.setCacheMapValue(key, message.getMessageId(),outBindIp);
        }
        String snKey = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(), "sn");
        String sn = jsonObject.getString("sn");
        if (StrUtil.isNotEmpty(sn)) {
            redisHandle.setCacheMapValue(snKey, sn, message.getMessageId());
            redisHandle.setCacheMapValue(snKey, message.getMessageId(), sn);
        }
    }

    private static void virtualHostEdit(ResMessageManage message, CommonFlowInfo info){
        JSONObject jsonObject = message.getRowData();
        String key = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(), "outbindIp");
        String outBindIp = jsonObject.getString("outbindIp");
        judgeUpdate(message, key, outBindIp);
        String snKey = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(), "sn");
        String sn = jsonObject.getString("sn");
        judgeUpdate(message,snKey,sn);
    }
    private static void virtualHostDel(ResMessageManage message, CommonFlowInfo info){
        String key = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(), "outbindIp");
        judgeDel(message, key);
        String snKey = buildKey(info.getCacheKeyPrefix(),info.getSourceTheme(), "sn");
        judgeDel(message, snKey);
    }

    private static void judgeDel(ResMessageManage message, String key) {
        String cacheMapValue = redisHandle.getCacheMapValue(key, message.getMessageId());
        redisHandle.deleteHashObject(key, message.getMessageId());
        if(StrUtil.isNotEmpty(cacheMapValue)){
            redisHandle.deleteHashObject(key,cacheMapValue);
        }
    }

    private static void judgeUpdate(ResMessageManage message, String key, String value) {
        String cacheValue = redisHandle.getCacheMapValue(key, message.getMessageId());
        if (StrUtil.isNotEmpty(value)) {
            if(StrUtil.isNotEmpty(cacheValue) &&!value.equals(cacheValue)){
                redisHandle.deleteHashObject(key,cacheValue);
                redisHandle.setCacheMapValue(key, value, message.getMessageId());
                redisHandle.setCacheMapValue(key, message.getMessageId(),value);
            }

        }
    }
}
